<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\SLessonFeedback;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LessonFeedbackController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('课后反馈')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('课后反馈')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('课后反馈')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('课后反馈')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SLessonFeedback);
		$aa = SLessonFeedback::get();

		$grid->id('Id')->sortable();
		$grid->lesson_id('课堂id');
		$grid->title('反馈标题');
		$grid->content('反馈内容');
		$grid->status('状态')->using(config("svs.feedback_status"));
		$grid->adminUser()->username('审核人');
		// $grid->admin_id()->display(function(){
		// return
		// });
		// $grid->adminUser()->username('审核人');
		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');
		//禁用导出
		$grid->disableExport();
		$grid->disableCreateButton();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SLessonFeedback::findOrFail($id));

		$show->id('Id');
		$show->lesson_id('课堂id');
		$show->title('反馈标题');
		$show->content('反馈内容');
		$show->status('状态')->as(function ($p) {
			return config("svs.feedback_status." . $p);
		});
		$show->admin_id('审核人');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');
		$show->panel()
			->tools(function ($tools) {
				// $tools->disableEdit();
				// $tools->disableList();
				$tools->disableDelete();
			});
		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SLessonFeedback);

		$form->tools(function (Form\Tools $tools) {

			// 去掉`列表`按钮
			// $tools->disableList();

			// 去掉`删除`按钮
			$tools->disableDelete();

			// 去掉`查看`按钮
			$tools->disableView();

		});

		$form->number('lesson_id', '课堂id')->disable();
		$form->text('title', '反馈标题')->disable();
		$form->text('content', '反馈内容')->disable();
		$form->select('status', '状态')->options(config("svs.feedback_status"));
		// $form->number('admin_id', 'Admin id');
		$form->hidden('admin_id')->value(\Admin::user()->id);


		$form->saved(function (Form $form) {

    		if( $form->model()->status == 2 ) {//通过审核
				dispatch(new \App\Jobs\AutoAddStuTask($form->model()->id,  \Admin::user()->id));

    		}
    					
			logger("add_stu_task");

		});

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});
		return $form;
	}
}
